package com.yupi.yuoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.exception.BusinessException;
import com.yupi.yuoj.model.dto.populartags.PopularTagsQueryRequest;
import com.yupi.yuoj.model.entity.PopularTags;
import com.yupi.yuoj.model.vo.PopularTagsVO;
import com.yupi.yuoj.service.PopularTagsService;
import com.yupi.yuoj.mapper.PopularTagsMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author admin
* @description 针对表【popular_tags(热门标签表)】的数据库操作Service实现
* @createDate 2025-11-01 21:27:49
*/
@Service
public class PopularTagsServiceImpl extends ServiceImpl<PopularTagsMapper, PopularTags>
    implements PopularTagsService{

    @Override
    public void validPopularTags(PopularTags popularTags, boolean add) {
        if (popularTags == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String tagName = popularTags.getTag_name();
        String category = popularTags.getCategory();
        Integer usageCount = popularTags.getUsage_count();

        // 创建时，参数不能为空
        if (add) {
            if (StringUtils.isAnyBlank(tagName, category)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        // 有参数则校验
        if (StringUtils.isNotBlank(tagName) && tagName.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标签名称过长");
        }
        if (StringUtils.isNotBlank(category) && category.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分类名称过长");
        }
        if (usageCount != null && usageCount < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "使用次数不能为负数");
        }
    }

    @Override
    public QueryWrapper<PopularTags> getQueryWrapper(PopularTagsQueryRequest popularTagsQueryRequest) {
        QueryWrapper<PopularTags> queryWrapper = new QueryWrapper<>();
        if (popularTagsQueryRequest == null) {
            return queryWrapper;
        }
        Long id = popularTagsQueryRequest.getId();
        String tagName = popularTagsQueryRequest.getTagName();
        String category = popularTagsQueryRequest.getCategory();
        Integer minUsageCount = popularTagsQueryRequest.getMinUsageCount();
        Integer maxUsageCount = popularTagsQueryRequest.getMaxUsageCount();
        String sortField = popularTagsQueryRequest.getSortField();
        String sortOrder = popularTagsQueryRequest.getSortOrder();

        // 拼接查询条件
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.like(StringUtils.isNotBlank(tagName), "tag_name", tagName);
        queryWrapper.eq(StringUtils.isNotBlank(category), "category", category);
        queryWrapper.ge(ObjectUtils.isNotEmpty(minUsageCount), "usage_count", minUsageCount);
        queryWrapper.le(ObjectUtils.isNotEmpty(maxUsageCount), "usage_count", maxUsageCount);
        queryWrapper.eq("is_deleted", false);
        queryWrapper.orderBy(StringUtils.isNotEmpty(sortField),
                sortOrder.equals("ascend"), sortField);
        return queryWrapper;
    }

    @Override
    public PopularTagsVO getPopularTagsVO(PopularTags popularTags, HttpServletRequest request) {
        PopularTagsVO popularTagsVO = PopularTagsVO.objToVo(popularTags);
        // 可以根据需要添加额外的业务逻辑
        return popularTagsVO;
    }

    @Override
    public Page<PopularTagsVO> getPopularTagsVOPage(Page<PopularTags> popularTagsPage, HttpServletRequest request) {
        List<PopularTags> popularTagsList = popularTagsPage.getRecords();
        Page<PopularTagsVO> popularTagsVOPage = new Page<>(popularTagsPage.getCurrent(), popularTagsPage.getSize(), popularTagsPage.getTotal());
        if (popularTagsList.isEmpty()) {
            return popularTagsVOPage;
        }
        // 填充信息
        List<PopularTagsVO> popularTagsVOList = popularTagsList.stream()
                .map(popularTags -> getPopularTagsVO(popularTags, request))
                .collect(Collectors.toList());
        popularTagsVOPage.setRecords(popularTagsVOList);
        return popularTagsVOPage;
    }
}




